package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;



    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    /**
     * 思路
     * user数据:User(id=null,username=abc,password=123,)
     * 1.将密码明文,转化为密文 sha1/md5算法/md5hash
     * 2.根据用户名和密码查询用户信息
     * 3.有结果: 返回token UUID
     * 4.没有结果: 返回null
     * @param user
     * @return
     * */
    @Override
    public String login(User user) {
        System.out.println(user);
       String password=user.getPassword();
       byte[]  bytes=password.getBytes();
       //1.将密码加密
       String md5Password= DigestUtils.md5DigestAsHex(bytes);
       user.setPassword(md5Password);
       //2.根据用户名和密码查询数据库

       User userDB=userMapper.findUserByUP(user);
       if(userDB==null){
           //用户名和密码查询错误
           return null;
       }
       //程序走到这里,说明用户名和密码正确 返回token
        String token= UUID.randomUUID().toString();
       return token;
    }

    @Override
    public PageResult findUserList(PageResult pageResult) {
        //1.总记录数
        long total=userMapper.findTotal();
        int start=(pageResult.getPageNum()-1)* pageResult.getPageSize();
        System.out.println(pageResult.getPageNum());
        System.out.println(start);
        int size=pageResult.getPageSize();
        String query=pageResult.getQuery();
        System.out.println(size);
        System.out.println(query);
        //Mybatis只支持单值传参 封装为map集合
        List<User>rows=userMapper.findUserList(start,size,query);
        pageResult.setRows(rows).setTotal(total);
        return pageResult;
    }

    @Override
    @Transactional
    public User updateStatus(User user) {
        user.setUpdated(new Date());//设定当前时间
        userMapper.updateStautas(user);
        return user;
    }

    @Override
    @Transactional
    public void deleteUserById(Integer id) {
        userMapper.deleteBuyId(id);
    }

    @Override
    //名称:@Transactional//事物的注解
    //特点:
    @Transactional//事物的注解
    public void saveUser(User user)  throws SQLException{
        String password=user.getPassword();
        byte[] bytes=password.getBytes();
        String md5Pass=DigestUtils.md5DigestAsHex(bytes);
        Date date=new Date();
        user.setPassword(md5Pass)
                .setStatus(true)
                .setCreated(date)
                .setUpdated(date);
        userMapper.saveUser(user);
        throw new SQLException();


}

    @Override
    public User findUserById(Integer id) {
        User user=userMapper.findUserById(id);
        return user;
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        user.setUpdated(new Date());
        userMapper.updateUser(user);
    }


}
